package com.twu.biblioteca.model;

import com.twu.biblioteca.dao.memory.MemoryBookDao;
import com.twu.biblioteca.entity.Book;
import com.twu.biblioteca.exception.CheckoutFailException;
import com.twu.biblioteca.exception.ReturnFailException;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.List;

import static org.fest.assertions.api.Assertions.assertThat;

public class BookModelIT {
  private static final long IsbnForBookToCheckout = 8941230131L;
  private BookModel sut;
  private Book bookToCheckout;

  @BeforeMethod
  public void setUp() throws Exception {
    sut = new BookModel(new MemoryBookDao());
    bookToCheckout = new Book(IsbnForBookToCheckout, "Pride and Prejudice", "Jane Austen");
  }

  @Test
  public void could_checkout_and_return_available_books() throws Exception {
    // Given

    // When
    List<Book> availableBooks = sut.checkout(bookToCheckout);

    // Then
    assertThat(availableBooks).hasSize(4);
  }

  @Test(expectedExceptions = CheckoutFailException.class)
  public void should_throw_exception_when_checkout_request_is_invalid() throws Exception {
    // Given

    // When
    sut.checkout(new Book(9941230131L, "Pride and Prejudice wrong", "Jane Austen wrong"));

    // Then
  }

  @Test(expectedExceptions = CheckoutFailException.class)
  public void could_not_checkout_not_available() throws Exception {
    // Given

    // When
    sut.checkout(bookToCheckout);
    sut.checkout(bookToCheckout);

    // Then
  }

  @Test
  public void could_return_borrowed_book() throws Exception {
    // Given

    // When
    sut.checkout(bookToCheckout);
    List<Book> availableBooks = sut.returnBook(bookToCheckout);

    // Then
    assertThat(availableBooks).hasSize(5);
  }

  @Test(expectedExceptions = ReturnFailException.class)
  public void should_throw_exception_when_return_request_is_invalid() throws Exception {
    // Given

    // When
    sut.returnBook(bookToCheckout);

    // Then
  }

}
